/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal.model;

import java.util.Comparator;

import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.resources.IChangelist;
import net.sourceforge.perforce.ui.IPerforceUIConstants;
import net.sourceforge.perforce.ui.PerforceUIPlugin;
import net.sourceforge.perforce.ui.internal.Policy;
import net.sourceforge.perforce.ui.model.PerforceModelElement;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
  * @version $Revision: 175 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class ClientPendingChangelistContainer extends PerforceModelElement 
                                              implements IWorkbenchAdapter, IAdaptable
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the Perforce Server. */
	private IPerforceServer server;
	/** Stores the Pending Changelist cache. */
	private PendingChangelistCache pendingChangelistCache;
	
	// ------------------------------------------------------------------------- Public Constructors
	
	/** Constructor for ClientPendingChangelistContainer. 
	  * 
	  * @param server reference to the Perforce server.
	  * @param cache the pending changelist cache.
	  */
	public ClientPendingChangelistContainer(IPerforceServer server, PendingChangelistCache cache)
	{
		this.server = server;
		pendingChangelistCache = cache;
	}

	// ---------------------------------------------------------- Public Methods (IWorkbenchAdapter)

	/** @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(Object) */
	public Object[] getChildren(Object o) 
	{
		if (!(o instanceof ClientPendingChangelistContainer))
		{
			return new Object[0];
		}
		
		ChangelistElement[] changelists = 
			pendingChangelistCache.getChangelistElements(
				new Comparator() 
				{
					public int compare(Object o1, Object o2) 
					{
						return 0;
					}
	
					public boolean equals(Object obj) 
					{
						IChangelist changelist = (IChangelist) obj;
						return changelist.getClientName().equals(server.getClient());
					}
				});
		
		return changelists;
	}

	/** @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(Object) */
	public ImageDescriptor getImageDescriptor(Object o) 
	{
		if (!(o instanceof ClientPendingChangelistContainer))
		{
			return null;
		}
		
		String imageId = IPerforceUIConstants.IMG_CLIENT_CHANGELISTS;
		return PerforceUIPlugin.getPlugin().getImageDescriptor(imageId);
	}

	/** @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(Object) */
	public String getLabel(Object o) 
	{
		if (!(o instanceof ClientPendingChangelistContainer))
		{
			return null;
		}
			
		return Policy.bind(
			"ClientPendingChangelistContainer.getLabel", server.getClient());      //$NON-NLS-1$
	}

	/** @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(Object) */
	public Object getParent(Object o) 
	{
		return null;
	}

	// ----------------------------------------------------------------- Public methods (IAdaptable)
	
	/** @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class) */
	public Object getAdapter(Class adapter) 
	{
		if (adapter == IWorkbenchAdapter.class)
		{
			return this;
		}
		else
		if (adapter == IPerforceServer.class)
		{
			return server;
		}
		
		
		return null;
	}
}
